package com.kitty.game.achieve.service;

import com.kitty.game.achieve.body.*;
import com.kitty.game.achieve.message.*;
import com.kitty.game.player.model.AchieveImgInfos;
import com.kitty.game.role.model.Role;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AchieveService {
    //查询成就  发送成就配置
    public void sendAchieveConfig(Role role,String type,boolean flag){
        RespAchieveConfig respAchieveConfig = new RespAchieveConfig();

        List<RespAchieveConfigDTO> respAchieveConfigDTOList = new ArrayList<>();
        AchieveImgInfos achieveImgInfos = role.getExtendBox().getAchieveImgInfos();
        if(achieveImgInfos == null){
            achieveImgInfos = new AchieveImgInfos();
            role.getExtendBox().setAchieveImgInfos(achieveImgInfos);
        }
        HashMap<String, Integer> achieveMap = achieveImgInfos.getAchieveMap();
        for (Map.Entry<String, AchieveSet> stringAchieveSetEntry : DataCache.ACHIEVE_DATA.entrySet()) {
            AchieveSet achieveSet = stringAchieveSetEntry.getValue();
//            String menuType = achieveSet.getType().substring(0,1);
//            String menuType2 = achieveSet.getType().substring(2,3);
//            if(achieveMap.containsKey(stringAchieveSetEntry.getKey())){
//
//            }else{
//                if(!"1".equals(menuType2) && !type.equals(menuType)){
//                    //continue;
//                }
//            }
            RespAchieveConfigDTO respAchieveConfigDTO = new RespAchieveConfigDTO();
            respAchieveConfigDTO.setId(Integer.parseInt(stringAchieveSetEntry.getKey()));
            respAchieveConfigDTO.setName(achieveSet.getTitle());
            respAchieveConfigDTO.setPoint(achieveSet.getScore());
            respAchieveConfigDTO.setProgress(0);
            respAchieveConfigDTO.setBonusDesc("");
            respAchieveConfigDTO.setAchieveDesc(achieveSet.getMsg());
            respAchieveConfigDTO.setCategory((short)Short.parseShort(achieveSet.getType()));
            respAchieveConfigDTO.setOrder((short)achieveSet.getScore());

            List<RespAchieveConfigListDTO> listDTOS = new ArrayList<>();
            RespAchieveConfigListDTO respAchieveConfigListDTO = new RespAchieveConfigListDTO();
            respAchieveConfigListDTO.setDes(achieveSet.getMsg());
            respAchieveConfigListDTO.setProcess((short)1);
            listDTOS.add(respAchieveConfigListDTO);
            //respAchieveConfigDTO.setListDTOS(listDTOS);
            respAchieveConfigDTOList.add(respAchieveConfigDTO);

        }
        respAchieveConfig.setRespAchieveConfigDTOList(respAchieveConfigDTOList);
        MessagePusher.pushMessage(role, respAchieveConfig);
    }


    //查询成就总览  发送成就
    public void sendAchieveAllView(Role role){
        RespAchieveOverView respAchieveOverView  = new RespAchieveOverView();
        respAchieveOverView.setBonusPoint(3000);
        respAchieveOverView.setBonusDesc("I#I称谓|成就达人#r称谓#I");
        respAchieveOverView.setCanBonus((byte)0);
        AchieveImgInfos achieveImgInfos = role.getExtendBox().getAchieveImgInfos();
        if(achieveImgInfos == null){
            achieveImgInfos = new AchieveImgInfos();
            role.getExtendBox().setAchieveImgInfos(achieveImgInfos);
        }
        respAchieveOverView.setTotal(achieveImgInfos.getTotalScore());
        respAchieveOverView.setTotalMax(600000);
        List<AchieveCategory> achieveCategoryList = new ArrayList<>();
        AchieveCategory achieveCategory1 = new AchieveCategory();
        achieveCategory1.setCategoryTotal(achieveImgInfos.getLvScore());
        achieveCategory1.setCategoryTotalMax(100000);
        AchieveCategory achieveCategory2 = new AchieveCategory();
        achieveCategory2.setCategoryTotal(achieveImgInfos.getFirendScore());
        achieveCategory2.setCategoryTotalMax(100000);
        AchieveCategory achieveCategory3 = new AchieveCategory();
        achieveCategory3.setCategoryTotal(achieveImgInfos.getEquipScore());
        achieveCategory3.setCategoryTotalMax(100000);
        AchieveCategory achieveCategory4 = new AchieveCategory();
        achieveCategory4.setCategoryTotal(achieveImgInfos.getRoleScore());
        achieveCategory4.setCategoryTotalMax(100000);
        AchieveCategory achieveCategory5 = new AchieveCategory();
        achieveCategory5.setCategoryTotal(achieveImgInfos.getTaskScore());
        achieveCategory5.setCategoryTotalMax(100000);
        AchieveCategory achieveCategory6 = new AchieveCategory();
        achieveCategory6.setCategoryTotal(achieveImgInfos.getExtScore());
        achieveCategory6.setCategoryTotalMax(100000);



        achieveCategoryList.add(achieveCategory1);
        achieveCategoryList.add(achieveCategory2);
        achieveCategoryList.add(achieveCategory3);
        achieveCategoryList.add(achieveCategory4);
        achieveCategoryList.add(achieveCategory5);
        achieveCategoryList.add(achieveCategory6);
        respAchieveOverView.setAchieveCategoryList(achieveCategoryList);

        List<AchieveInfo> achieveInfoList = new ArrayList<>();
        for (Map.Entry<String, Integer> stringIntegerEntry : achieveImgInfos.getAchieveMap().entrySet()) {
            AchieveInfo achieveInfo  = new AchieveInfo();
            achieveInfo.setId(Integer.parseInt(stringIntegerEntry.getKey()));
            achieveInfo.setTime(stringIntegerEntry.getValue());
            achieveInfoList.add(achieveInfo);
        }
        respAchieveOverView.setAchieveInfoList(achieveInfoList);
        MessagePusher.pushMessage(role, respAchieveOverView);
    }
    //查询某个类型成就
    public void queryAchieveByType(Role role, ReqAchieveView reqAchieveView){
        byte category = reqAchieveView.getCategory();
        RespAchieveView respAchieveView = new RespAchieveView();
        respAchieveView.setCategory(category);
        List<RespAchieveViewDTO> respAchieveViewDTOList = new ArrayList<>();

        getAchieveMap(category+"",respAchieveViewDTOList,role);
        respAchieveView.setRespAchieveViewDTOList(respAchieveViewDTOList);
        MessagePusher.pushMessage(role, respAchieveView);
    }

    public static void main(String[] args) {
        String a = "101";
        System.out.println(a.substring(2,3));
    }
    /**
     *
     * @param type
     * @param respAchieveViewDTOList
     */
    private void getAchieveMap(String type,List<RespAchieveViewDTO> respAchieveViewDTOList,Role role){
        AchieveImgInfos achieveImgInfos = role.getExtendBox().getAchieveImgInfos();
        HashMap<String, Integer> achieveMap = achieveImgInfos.getAchieveMap();
        for (Map.Entry<String, AchieveSet> stringAchieveSetEntry : DataCache.ACHIEVE_DATA.entrySet()) {
            AchieveSet achieveSet = stringAchieveSetEntry.getValue();
            //查询对应类型的成就
            String type1 = achieveSet.getType();
            type1 = type1.substring(0,1);
            if(type.equals(type1)){
                RespAchieveViewDTO respAchieveViewDTO = new RespAchieveViewDTO();
                respAchieveViewDTO.setAchieveId(Integer.parseInt(stringAchieveSetEntry.getKey()));
                if(achieveMap.containsKey(stringAchieveSetEntry.getKey())){
                    //成就完成时间
                    Integer integer = achieveMap.get(stringAchieveSetEntry.getKey());
                    respAchieveViewDTO.setIsFinished((byte)1);
                    respAchieveViewDTO.setProgressOrTime(integer);
                }else{
                    respAchieveViewDTO.setIsFinished((byte)0);
                    respAchieveViewDTO.setProgressOrTime(0);
                }
//                List<RespAchieveConfigListDTO> listDTOS = new ArrayList<>();
//                RespAchieveConfigListDTO respAchieveConfigListDTO = new RespAchieveConfigListDTO();
//                respAchieveConfigListDTO.setDes(achieveSet.getMsg());
//                respAchieveConfigListDTO.setProcess((short)achieveSet.getScore());
//                listDTOS.add(respAchieveConfigListDTO);
//                respAchieveViewDTO.setListDTOS(listDTOS);
                respAchieveViewDTOList.add(respAchieveViewDTO);
            }
        }

    }
}
